<?php
class Materials extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->helper('url_helper');
		$this->load->helper('gd_helper');     
		$this->load->model('Userinfo_model');		
        $this->load->model('Materials_model');  
	}

	//建材库中心
	public function index(){
		// 建材分类
        $catlist =$this->Materials_model->getmaterialscat();
        foreach ($catlist as $key => $value) {
            $key =$key+1;
            $catlist[0]['id']=0;
            $catlist[0]['catname'] ='全部';
            $catlist[0]['choose'] =true;
            $value['choose']=false;
            $catlist[$key]=$value;
        }

        $goodslist=$this->Materials_model->getmaterialsBycatId(0,0,12);
        foreach ($goodslist as $key => $value) {
            if($value['typetag'] ==1){
               $goodslist[$key]['typetagname'] ='严选';
            }else{
               $goodslist[$key]['typetagname'] ='自选';
            }
            switch ($value['saletag']) {
                case '1':
                    $goodslist[$key]['saletagname'] ='正价';
                    break;
                case '2':
                    $goodslist[$key]['saletagname'] ='折扣';
                    break;
                default:
                    $goodslist[$key]['saletagname'] ='特价';
                    break;
            }
        }

        $count =$this->Materials_model->getmaterialscount(0);
        $totalpage =ceil($count/12);
        $data['eachlist'] =json_encode($goodslist);
        $data['catlist'] =json_encode($catlist);
        $data['totalpage'] =$totalpage;
		$this->load->view('web/materials',$data);
	}
	// 产品详情
	public function materialsdetail(){
		$id = $this->input->get('id');
        $info =$this->Materials_model->getmaterialsById($id);
        if(!$info){
            Header("Location:/HomePage/index");exit;
        }
        $imgs =$info['imgs'];
        $caseinfos =json_decode($info['caseinfos'],true);
        foreach ($caseinfos as $key => $value) {
            $value ='https://yunying.jzjgds.com/upload/'.$value;
            $caseinfos[$key] =$value;
        }
        $data['imgs']=$imgs;
        $data['caseinfos']=json_encode($caseinfos);
        $data['info'] =$info;
		$this->load->view('web/materialsdetail',$data);
	}

	// 分页获取数据
	public function ajax_getmaterialslist(){
		$catid = $this->input->post('catid');
        $page = $this->input->post('page');
        $from = ($page - 1) * 12;
        $to = 12;
        $goodslist=$this->Materials_model->getmaterialsBycatId($catid,$from,$to);
        foreach ($goodslist as $key => $value) {
            if($value['typetag'] ==1){
               $goodslist[$key]['typetagname'] ='严选';
            }else{
               $goodslist[$key]['typetagname'] ='自选';
            }
            switch ($value['saletag']) {
                case '1':
                    $goodslist[$key]['saletagname'] ='正价';
                    break;
                case '2':
                    $goodslist[$key]['saletagname'] ='折扣';
                    break;
                default:
                    $goodslist[$key]['saletagname'] ='特价';
                    break;
            }
        }
        $count =$this->Materials_model->getmaterialscount($catid);
        $totalpage =ceil($count/12);
        $arr =array('goodslist'=>$goodslist,'totalpage'=>$totalpage);
        die(json_encode($arr));
	}



    public function chatlogin(){
        $this->load->view('web/chatlogin');
    }
    
    public function chat(){
        $this->load->view('web/chat');
    }
	public function publish(){
	    $this->load->view('web/publish');
	}
	public function subscribe(){
	    $this->load->view('web/subscribe');
	}
}